<script setup>

import { ref, computed, onMounted } from 'vue'
import { httpUtils } from '@/utils/http'

const list = ref([])
const searchName = ref('')
const currentPage = ref(1)
const pageSize = 10
const totalPages = ref(0)
const total = ref(0)

async function fetchData() {
    httpUtils.post('emp/list', {
        name: searchName.value,
        pageNum: currentPage.value,
        pageSize: pageSize
    }).then(res => {
        console.log(res)
        list.value = res.records
        totalPages.value = res.pages
        total.value = res.total
        currentPage.value = res.current
    })
}
const showAdd = () => {
    location.href = '/add'
}
const handleSearch = () => {
    currentPage.value = 1
    fetchData()
}
const prevPage = () => {
    currentPage.value--
    fetchData()
}
const nextPage = () => {
    currentPage.value++
    fetchData()
}
const deleteEmp = (id) => {
    const flag = confirm(`确定删除${id}号员工吗？`)
    if (!flag) {
        return
    }
    httpUtils.post('emp/delete?empId=' + id).then(res => {
        console.log(res)
        if (res.code == 0) {
            fetchData()
        } else {
            alert('删除失败')
        }
    }).catch(err => {
        alert('删除失败')
    })
}

const updateEmp = (id) => {
    location.href = '/update?id=' + id
}

onMounted(() => {
    fetchData()
})
</script>

<template>
    <div id="container">
        <div id="content">
            <!-- 查询框区域 -->
            <div style="margin-bottom: 16px;" id="search">
                用户名：<input v-model="searchName" placeholder="请输入查询内容" />
                <button @click="handleSearch">查询</button>
            </div>
            <div style="margin-bottom: 16px;" id="add">
                <button @click="showAdd">添加</button>
            </div>

            <!-- 列表区域（使用 table） -->
            <table border="1" cellpadding="8" cellspacing="0" style="width: 100%;">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>名称</th>
                        <th>年龄</th>
                        <th>性别</th>
                        <th>电子邮箱</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <tr v-for="item in list" :key="item.id">
                        <td>{{ item.empId }}</td>
                        <td>{{ item.empName }}</td>
                        <td>{{ item.age }}</td>
                        <td>{{ item.sex }}</td>
                        <td>{{ item.email }}</td>
                        <td>
                            <button @click="updateEmp(item.empId)">修改</button>&nbsp
                            <button @click="deleteEmp(item.empId)">删除</button>
                        </td>
                    </tr>
                </tbody>
            </table>

            <!-- 分页区域 -->
            <div style="margin-top: 16px;" id="pagination">
                <button :disabled="currentPage === 1" @click="prevPage">上一页</button>
                <span>第 {{ currentPage }} 页 / 共 {{ totalPages }} 页/共 {{ total }} 条</span>
                <button :disabled="currentPage === totalPages" @click="nextPage">下一页</button>
            </div>
        </div>
    </div>


</template>


<style scoped>
#container {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    min-height: 100vh;
    background: #f7f8fa;
    padding: 40px 0;
}

#content {
    background: #fff;
    border-radius: 10px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
    padding: 32px 40px;
    min-width: 700px;
}

#search {
    display: flex;
    align-items: center;
    gap: 8px;
}

#search input {
    padding: 6px 10px;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
    outline: none;
    transition: border-color 0.2s;
}

#search input:focus {
    border-color: #409eff;
}

#search button,
#add button,
td button {
    background: #409eff;
    color: #fff;
    border: none;
    border-radius: 4px;
    padding: 6px 18px;
    cursor: pointer;
    transition: background 0.2s;
}

#search button:hover,
#add button:hover {
    background: #66b1ff;
}

table {
    border-collapse: collapse;
    width: 100%;
    background: #fff;
    margin-top: 8px;
}

th,
td {
    text-align: center;
    padding: 12px 8px;
}

thead th {
    background: #f2f6fc;
    color: #333;
    font-weight: 600;
    border-bottom: 2px solid #ebeef5;
}

tbody tr {
    transition: background 0.2s;
}

tbody tr:hover {
    background: #f5f7fa;
}

tbody td {
    border-bottom: 1px solid #ebeef5;
}

#pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    margin-top: 24px;
}

#pagination button {
    background: #409eff;
    color: #fff;
    border: none;
    border-radius: 4px;
    padding: 6px 16px;
    cursor: pointer;
    transition: background 0.2s;
}

#pagination button:disabled {
    background: #c0c4cc;
    cursor: not-allowed;
}

#pagination span {
    color: #606266;
    font-size: 15px;
}
</style>